﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.StorageClient;

namespace Microsoft.WindowsAzure.Search
{
    public class Context
    {
        public static List<String> GetContextLines(CloudBlobContainer dataContainer, List<String> QueryTerms, List<String> result)
        {
            List<String> contextLines = new List<String>();

            foreach (String url in result)
            {
                CloudBlob blob = dataContainer.GetBlobReference(url);
                String content = blob.DownloadText();
                String contextLine = "";

                bool found = false;
                String[] lines = Tokenizer.LineTokenize(content);
                foreach (String line in lines)
                {
                    foreach (String term in QueryTerms)
                    {
                        String lineCopy = line.ToLower();
                        if (lineCopy.IndexOf(term) != -1)
                        {
                            lineCopy = Filter.NonwordsFilter(lineCopy);
                            String[] words = Tokenizer.WhitespaceTokenize(lineCopy);
                            if (words.Contains(term))
                            {
                                contextLine = line;
                                found = true;
                                break;
                            }
                        }
                    }

                    if (found)
                        break;
                }

                contextLines.Add(contextLine);
                
            }
            return contextLines;
        }
    }
}